<?php
/**
 * Created by PhpStorm.
 * UsersF: Mloong
 * Date: 2018/12/10
 * Time: 9:45
 */

namespace app\admin\controller;


use app\admin\service\AdminService;
use app\admin\validate\addAdmin;
use app\admin\model\Admin as AdminModel;
use think\facade\Request;

class Admin extends BaseController
{
    public function index($id = null)
    {
        if($id !== null){
            $findOne = AdminModel::findOne($id);
            $this->assign('info',$findOne);
        }
        return $this->fetch('admin/addAdmin');
    }
    public function addAdmin()
    {
        $post_data = Request::post();
//        return json($post_data);
        $validate = new addAdmin();
        if (!$validate->check($post_data)) {
            $this->error($validate->getError());
        }
        $service  = new AdminService();
        $result = $service->addAdmin($post_data);
        if($result['success'] == true){
            $this->success($result['msg']);
        }else{
            $this->error($result['msg']);
        }
    }
    public function adminList()
    {
        return $this->fetch('admin/admin_list');
    }
    public function getAllList()
    {
        $post_data = Request::param();
        $service = new AdminService();
        $result = $service->getAllAdminList($post_data);
        return json($result);
    }
    public function delAdmin($id)
    {
        $result = AdminModel::delOne($id);
        if($result){
            return json([
                'code' =>200,
                'msg' => '删除成功'
            ]);
        }else{
            return json([
                'code' =>400,
                'msg' => '删除失败'
            ]);
        }
    }
}